/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> tmp;
        if(root==nullptr){return tmp;}
        int n=getdepth(root);
        vector<vector<int>> ans(n);
        dfs(root,0,ans);
        std::reverse(ans.begin(),ans.end());
        return ans;
    }
    void dfs(TreeNode* root,int level,vector<vector<int>>&ans){
        if(root==nullptr){return;}
        ans[level].push_back(root->val);
        if(root->left!=nullptr){dfs(root->left,level+1,ans);}
        if(root->right!=nullptr){dfs(root->right,level+1,ans);}
    }
    int getdepth(TreeNode* root){
        if(root==nullptr){return 0;}
        else{return max(getdepth(root->left)+1,getdepth(root->right)+1);}
    }
};